package com.gglx.mobile.extenstions
{
	import com.distriqt.extension.pushnotifications.PushNotifications;
	import com.distriqt.extension.pushnotifications.events.PushNotificationEvent;
	import com.gglx.GlobalConfig;
	import com.gglx.GlobalRegister;
	import com.gglx.LocalAppData;
	import com.gglx.net.GGURLLoader;

	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.net.URLVariables;

	public class GGPushNotification
	{
		private static const GCM_SENDER_ID:String="947439654272";

		public function GGPushNotification()
		{
		}

		public static function init():void
		{
			try
			{
				PushNotifications.init(GlobalConfig.DEV_KEY);

				trace("Push Notifiction Supported: " + String(PushNotifications.isSupported));
				trace("Push Notifiction Version: " + PushNotifications.service.version);

				if (PushNotifications.isSupported)
				{
					PushNotifications.service.addEventListener(PushNotificationEvent.REGISTER_SUCCESS, registerSuccessHandler);
					PushNotifications.service.addEventListener(PushNotificationEvent.UNREGISTERED, unregisterSuccessHandler);
					PushNotifications.service.addEventListener(PushNotificationEvent.NOTIFICATION, notificationHandler);
					PushNotifications.service.addEventListener(PushNotificationEvent.ERROR, errorHandler);

					if (PushNotifications.service.getDeviceToken() == "")
					{
						/*
						Đăng ký nhận push với Google Cloud Message
						//*/
						PushNotifications.service.register(GCM_SENDER_ID);
					}
				}
			}
			catch (e:Error)
			{
				trace(e.message);
			}
		}

		private static function registerSuccessHandler(event:PushNotificationEvent):void
		{
			// push notification register id
			var pNRegId:String=event.data;
			trace("PN registration succeeded with reg ID: \n" + pNRegId);

			//*
			// Đăng ký mã push lên server của mình để broadcast
			var registerVar:URLVariables=new URLVariables();
			registerVar.data='{"method":"reg","params":{"cuid":"' + pNRegId + '","sysProp":"' + GlobalRegister.SYSTEM_PROPERTIES + '"},"id":1}';

			var registerRequest:URLRequest=new URLRequest(GlobalConfig.GCM_REG_URL);
			registerRequest.data=registerVar;

			var registerLoader:GGURLLoader=new GGURLLoader(registerRequest);
			registerLoader.result=registerResult;

			LocalAppData.CONFIG.cupid=pNRegId;
			//*/
		}

		private static function registerResult(data:Object):void
		{
			var result:Object=JSON.parse(data.toString());

			if (result.error == null)
			{
				LocalAppData.CONFIG.isRegistered=true;

				LocalAppData.writeFile();
			}
		}

		private static function unregisterSuccessHandler(event:PushNotificationEvent):void
		{
			trace("PN unregistration succeeded: \n" + event.data);
		}

		private static function notificationHandler(event:PushNotificationEvent):void
		{
			trace("Remote notification received!\n" + event.data);
		}

		private static function errorHandler(event:PushNotificationEvent):void
		{
			trace("ERROR::" + event.data);
		}
	}
}
